#include "mygraphicsview.h"

MyGraphicsView::MyGraphicsView(QWidget *parent)
    : QGraphicsView{parent}
{
    setScene(new QGraphicsScene(this));
    setInteractive(true);
    setSceneRect(-500000, -500000, 1000000, 1000000);
    // setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

void MyGraphicsView::setDraw(bool state)
{
    m_isDrawing = state;
}

bool MyGraphicsView::getDraw()
{
    return m_isDrawing;
}

QGraphicsPolygonItem *MyGraphicsView::getPolygonItem()
{
    return m_polygonItem;
}

void MyGraphicsView::setmask(Mat *mask)
{
    m_mask = mask;
}

void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if(m_isDrawing)
    {
        if(event->button() & Qt::LeftButton)
        {
            if(m_polygonItem == nullptr)
            {
                QPolygonF polygonf;
                polygonf << mapToScene(event->pos());
                m_polygonItem = scene()->addPolygon(polygonf, QPen(Qt::cyan, 2, Qt::DotLine));
            }
            else
            {
                QPolygonF polygonf = m_polygonItem->polygon();
                polygonf << mapToScene(event->pos());   //  把所有点击的点放入多边形里
                m_polygonItem->setPolygon(polygonf);
            }
        }
        else if(event->button() & Qt::RightButton)
        {
            Polygon* polygon;
            if(m_polygonItem)
            {
                polygon = new Polygon(getPolygonItem()->polygon());
                polygon->setContextMenuEnabled(false);  //  取消右键菜单

                connect(polygon, &ROI::upROIMask, this, &MyGraphicsView::updateROI);
                scene()->addItem(polygon);
                ROI::setMask(scene()->items(), *m_mask);
                scene()->removeItem(m_polygonItem);
                m_polygonItem = nullptr;
                event->ignore();
            }
            m_isDrawing = false;
            return ;
        }
    }
    QGraphicsView::mousePressEvent(event);
}

void MyGraphicsView::mouseDoubleClickEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);   // 图片自适应窗口大小
        m_angle = 0;
    }
    QGraphicsView::mouseDoubleClickEvent(event);
}

void MyGraphicsView::wheelEvent(QWheelEvent *event)
{
    int angle = event->angleDelta().y();  // 滚轮角度
    m_angle += angle;

    if(m_angle > 120*15)
        m_angle = 120*16;
    else if(m_angle < -120*4)
        m_angle = -120*5;
    if(m_angle >120*15 || m_angle < -120*4)
        return ;

    qreal scaleFactor = std::pow(2.0, angle/240.0);     // 缩放因子

    QPointF mousePos = mapToScene(event->position().toPoint());
    scale(scaleFactor, scaleFactor);        // 缩放
    QPointF mousePosAferScale = mapFromScene(mousePos); // 缩放后鼠标的位置，场景坐标->视图坐标
    QPointF delta = event->position() - mousePosAferScale;

    Translate(delta);
}

void MyGraphicsView::Translate(QPointF &delta)
{
    int w = viewport()->rect().width();
    int h = viewport()->rect().height();
    QPoint newCenter(w/2.-delta.x()+0.5*m_symbol, h/2.-delta.y()+0.5*m_symbol);  // 0.5是向上取整
    centerOn(mapToScene(newCenter));        // 将视图坐标转换为场景坐标
}
